import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/grids/studentannouncementDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

class StudentMyAnnouncements extends StatefulWidget {
  static const String routeName = '/student/myannouncements';

  @override
  State<StudentMyAnnouncements> createState() => _StudentMyAnnouncementsState();
}

class _StudentMyAnnouncementsState extends State<StudentMyAnnouncements> {
  final box = Hive.box('appconfig');
  final SchoolAdminController controller = Get.find();
  final UserController _userController = Get.find();
  String _classId = '';

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    _classId = box.get(CacheManagerKey['currentclass'].toString());
    Future.delayed(Duration(milliseconds: 100), (() {
      if (isStudent) {
        controller.fetchStudentClassAnnouncementsByClassId(_classId);
      }
    }));
    super.initState();
  }

  @override
  void dispose() {
    controller.studentannouncements.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student Announcements Management');

    return Scaffold(
      appBar: CustomAppBar(
        title: 'My Announcements'.tr,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            controller.obx(
              (data) => Expanded(
                child: StudentAnnouncementsDataGrids(
                    _userController.currentStudentClass,
                    controller.studentannouncements),
              ),
              onEmpty: Center(
                  child:
                      Text('You have not received any announcement yet!'.tr)),
            ),
          ],
        ),
      ),
    );
  }
}
